// pages/zhuce/zhuce.js
var app = getApp();
Page({
    //页面的初始数据
    data: {
        dialogShow: false,
        buttons: [{text: '男'}, {text: '女'}],
        isAgree: false,
        qpassword:null,
        yd:false,
        username:null,
        password:null,
        phone:null,
        city:null,
        banji:null,
        birth:"请选择你的生日",
        xingbie:null,
        openid:null,
        customItem: '全部',
        gender:"",
        place:"",
        region: ['选择', '你的', '地区']
    },
    bindRegionChange: function (e) {
        this.setData({
          region: e.detail.value
        })
        this.data.city=this.data.region[0]+this.data.region[1]+this.data.region[2];
        console.log( this.data.city);
      },
    //显示生日
    bindDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          birth: e.detail.value
        })
        console.log(this.data.birth);
      },
    //弹窗
    openConfirm: function () {
        this.setData({
            dialogShow: true
        })
    },
    tapDialogButton(e) {
        this.setData({
            dialogShow: false,
            showOneButtonDialog: false
        })
        console.log(e.detail.item);
        if(e.detail.item.text=="男")
        {
            this.setData({
                gender:"男"
            })
            this.getgender(1);
        }
        else{
            this.setData({
               gender:"女"
            })
            this.getgender(0);
        }
    },
        //获取修改的用户名
   getusername(e)
   {
       this.data.username=e.detail.value;
   },
   getbirth(b)
{
    this.data.birth=b.detail.value;
},
//获取修改的性别
getgender(g)
{
    this.data.xingbie=g;
},
//获取修改的班级
getbanji(b)
{
    this.data.banji=b.detail.value;                                         
},
    //获取手机号
    getphone(e)
    {
       if (!(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(e.detail.value))) {
        wx.showToast({
        title: '手机号码有误',
        duration: 1000,
        icon:'none'
        });
    }
    else{
        wx.showToast({
            title: '手机号码正确',
            duration: 1000,
            icon:'none'
            });
        this.data.phone=e.detail.value;
    }
    },
    //获取密码
    gopassword(e)
    {
      this.data.password=e.detail.value;
        
    },
    //获取确认密码
    go1password(q)
    {
       this.data.qpassword=q.detail.value;
    },
    //同意条款
    bindAgreeChange: function (e) {
        this.setData({
            isAgree: !!e.detail.value.length
        });
        this.data.yd=!this.data.yd;
    },
    //注册
    submitForm()
    {
        let that=this;
        console.log(this.data.yd);
        if(this.data.yd==undefined||this.data.yd==false)
        {
            wx.showToast({
                title: '未同意条款',
                icon: 'none',
                duration: 1500
              })
        }
      else{
        if(this.data.password!=that.data.qpassword)
        {
            wx.showToast({
                title: '密码不一致！',
                icon: 'none',
                duration: 1500
              })
        }
        else{
            that.getuseropenid();
        }
      }
    },
    //获取openid
    getuseropenid()
    {
        let that=this;
        wx.cloud.callFunction({
            name:'getuserid'
        }).then(res=>{
            console.log("成功",res.result.openid);
            this.data.openid=res.result.openid;
            that.adduser();
        })
        .catch(res=>{
            console.log("fail",res);
        })
    },
    //添加用户
    adduser()
    {
        let that=this;
       wx.request({
        url:app.globalData.url+'/insert', 
        header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
        data: {
            openid: that.data.openid,
            xingbie:that.data.xingbie,
            username:that.data.username,
            password:that.data.password,
            phone:that.data.phone,
            birth:that.data.birth,
            city:that.data.city,
            honor:"暂无",
            banji:that.data.banji
         },
         method: 'post',
         success: function (res) {
             if(res.data==1)
             {
                app.globalData.openid=res.data.openid;
                wx.setStorageSync('openid', res.data.openid);
                wx.setStorageSync('username', that.data.username);
                wx.showToast({
                  title: '注册成功!',
                  duration:1000
                });
                setTimeout(that.gouser,1000);
             }
             else
             {
                 wx.showToast({
                   title: '注册失败，请重试！',
                   icon:"error",
                   duration:1000
                 })
             }
         },
         fail:function(err){//请求失败之后要做什么
            console('请求失败')
            }
    })
    },
      gouser()
      {
        wx.switchTab({
            url: '/pages/user/user',
          })
      } 
})